package com.api.controller

import com.api.rpc.RPC_Service1
import com.api.rpc.RPC_Socket
import com.base.model.Model_API
import com.base.model.Model_MyBatisDB
import com.base.model.Model_Redis
import com.base.model.Model_Service1
import com.base.service.IRPC_API
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand
import com.yfree.global.YController
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.web.bind.annotation.CrossOrigin
import org.springframework.web.bind.annotation.RestController

@RestController
@CrossOrigin
@DefaultProperties(defaultFallback = "yfree_defaultFallback")
class Controller_API : YController(), IRPC_API {
    @Value("\${yangzhe.config1}")
    lateinit var config1: String

    @Value("\${yangzhe.config2}")
    lateinit var config2: String

    @Value("\${apollo-key:default-value}")
    lateinit var apolloKey: String

    @Autowired
    lateinit var rpc_Service1: RPC_Service1

    @Autowired
    lateinit var rpc_Socket: RPC_Socket

    @HystrixCommand
    override fun index(): String {
        return json()
    }

    @HystrixCommand
    override fun `interface`(jkid: String, jkparam: String?): String {
        val params = params(jkparam, Map::class.java)
        val apiModel = Model_API()
        return when (jkid) {
            "00A00" -> { // 模拟出参
                apiModel.key = "value"
                json(result = arrayListOf(apiModel))
            }
            "00A01" -> { // 发送socket
                val param_socketID = params["socketID"].toString()
                val param_message = params["message"].toString()
                return rpc_Socket.send(param_socketID, param_message)
            }
            "00A02" -> { // MySQL
                val param_id = params["id"].toString()
                val yModel = fromJson(rpc_Service1.db_MySQL(param_id), Model_MyBatisDB::class.java)
                return json(result = arrayListOf(yModel.root.result.first()))
            }
            "00A03" -> { // MongoDB
                val param_id = params["id"].toString()
                val yModel = fromJson(rpc_Service1.db_MongoDB(param_id), Model_MyBatisDB::class.java)
                return json(result = arrayListOf(yModel.root.result.first()))
            }
            "00A04" -> { // Redis
                val param_id = params["id"].toString()
                val yModel = fromJson(rpc_Service1.db_Redis(param_id), Model_Redis::class.java)
                return json(result = arrayListOf(yModel.root.result.first()))
            }
            "00A05" -> { // 获取自定义配置
                apiModel.config1 = config1
                apiModel.config2 = config2
                apiModel.apolloKey = apolloKey
                json(result = arrayListOf(apiModel))
            }
            "00A06" -> { // Ribbon
                val yModel = fromJson(rpc_Service1.callService2_rest(), Model_Service1::class.java)
                return json(result = arrayListOf(yModel.root.result.first()))
            }
            "00A07" -> { // Feign
                val yModel = fromJson(rpc_Service1.callService2_feign(), Model_Service1::class.java)
                return json(result = arrayListOf(yModel.root.result.first()))
            }
            else -> json(404, message = "jkid不存在")
        }
    }
}
